package demo.database.H2.D01_功能测试;

import java.util.concurrent.CountDownLatch;

import com.titan.core.utils.common.interfaces.ICall;
import com.titan.core.utils.db.JdbcReturnDomain;
import com.titan.core.utils.db.JdbcUtils;
import com.titan.core.utils.io.ConsoleUtils;
import com.titan.core.web.options.RunMainOption;
import com.titan.core.web.utils.SpringBootUtils;

import demo.database.H2.utils.H2Utils;

public class T03_插入自增长字段后返回 implements ICall {
    public static final String TABLE_NAME = "aa";
    
    
    public void call() throws Exception {
        JdbcUtils.setDataSource(H2Utils.getDataSource());
        if(!JdbcUtils.checkTableExist(TABLE_NAME)) {
            JdbcUtils.update("DROP TABLE IF EXISTS `"+TABLE_NAME+"`");
            JdbcUtils.update("CREATE TABLE `"+TABLE_NAME+"` (`id` int(10) PRIMARY KEY AUTO_INCREMENT, `data` int, `create_dt` datetime)");
        }
        
        for (int i = 0; i < 5; i++) {
            String sql = "INSERT INTO `"+TABLE_NAME+"`(`data`,`create_dt`) VALUES (?, NOW())";
            JdbcReturnDomain ret = JdbcUtils.updateAndReturnKey(sql, i);
            ConsoleUtils.printPart(5, "打印结果["+i+"]"
                    , "rows", ret.getRows()
                    ,  "key", ret.getKeyHolder().getKey()
                    );
        }
        
        EXIT.countDown();
    }
    
    
    public static void main(String[] args) throws InterruptedException {
        RunMainOption option = RunMainOption.$().addStartupArgs(
                "--titan.mode=development"   // # development configuration production test
              , "--titan.log.DateFile.enabled=false"
              , "--titan.node.home=E:\\HDJT"
        );
        SpringBootUtils.runMain(option, new T03_插入自增长字段后返回(), EXIT, EXIT_TIMEOUT_SECONDS);
    }
    private static final CountDownLatch EXIT = new CountDownLatch(1);
    private static final int EXIT_TIMEOUT_SECONDS = 3;
}
